home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / bitmap / dialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  6.0 KB  |  201 lines

  1. /*
  2.  * $XConsortium: Dialog.c,v 1.10 91/07/24 15:46:20 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Davor Matic, MIT X Consortium
  24.  */
  25.  
  26. #ifdef MSDOS
  27. #include "X11/Intrinsc.h"      /* QDK 05/11/1994 10:44am. */
  28. #else
  29. #include "X11/Intrinsic.h"
  30. #endif
  31. #include <X11/StringDefs.h>
  32. #include <X11/Shell.h>
  33. #include <X11/Xaw/Dialog.h>
  34. #include <X11/Xaw/Command.h>
  35. #include <X11/Xmu/CharSet.h>
  36.     
  37. #include "Dialog.h"
  38.  
  39. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  40. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  41.  
  42. static void SetDialogButton();
  43.  
  44. static XtActionsRec actions_table[] = {
  45.   {"set-dialog-button", SetDialogButton},
  46. };
  47.  
  48. static DialogButton dialog_buttons[] = {
  49.     {"yes", Yes},
  50.     {"no", No},
  51.     {"maybe", Maybe},
  52.     {"okay", Okay},
  53.     {"abort", Abort},
  54.     {"cancel", Cancel},
  55.     {"retry", Retry},
  56. };
  57.  
  58. static unsigned long selected;
  59.  
  60. /* ARGSUSED */
  61. static void SetSelected(w, clientData, callData) /* ARGSUSED */
  62.      Widget w;
  63.      XtPointer clientData, callData;
  64. {
  65.     String name = (String)clientData;
  66.     int i;
  67.     
  68.     for (i = 0; i < XtNumber(dialog_buttons); i++) 
  69.     if (!strcmp(dialog_buttons[i].name, name)) 
  70.         selected |= dialog_buttons[i].flag;
  71. }
  72.  
  73. /* ARGSUSED */
  74. static void SetDialogButton(w, event, argv, argc)
  75.      Widget w;         /* not used */
  76.      XEvent *event;    /* not used */
  77.      String *argv;
  78.      Cardinal *argc;  
  79. {
  80.   char button_name[80];
  81.   XtPointer dummy = NULL;
  82.   int i;
  83.  
  84.   for (i = 0; i < *argc; i++) {
  85.     XmuCopyISOLatin1Lowered (button_name, argv[i]);
  86.     SetSelected(w, button_name, dummy);
  87.   }
  88. }
  89.  
  90. static Boolean firstTime = True;
  91.  
  92. Dialog CreateDialog(top_widget, name, options)
  93.      Widget top_widget;
  94.      String name;
  95.      unsigned long options;
  96. {
  97.     int i;
  98.     Dialog popup;
  99.  
  100.     popup = (Dialog) XtMalloc(sizeof(_Dialog));
  101.  
  102.     if (popup) {
  103.         if (firstTime) {
  104.       XtAddActions(actions_table, XtNumber(actions_table));
  105.       firstTime = False;
  106.     }
  107.     popup->top_widget = top_widget;
  108.     popup->shell_widget = XtCreatePopupShell(name, 
  109.                          transientShellWidgetClass, 
  110.                          top_widget, NULL, 0);
  111.     popup->dialog_widget = XtCreateManagedWidget("dialog", 
  112.                              dialogWidgetClass,
  113.                              popup->shell_widget, 
  114.                              NULL, 0);
  115.     for (i = 0; i < XtNumber(dialog_buttons); i++)
  116.         if (options & dialog_buttons[i].flag)
  117.         XawDialogAddButton(popup->dialog_widget, 
  118.                    dialog_buttons[i].name, 
  119.                    SetSelected, dialog_buttons[i].name);
  120.     popup->options = options;
  121.     return popup;
  122.     }
  123.     else
  124.     return NULL;
  125. }
  126.  
  127. void PopdownDialog(popup, answer)
  128.     Dialog popup;
  129.     String *answer;
  130. {
  131.     if (answer)
  132.     *answer = XawDialogGetValueString(popup->dialog_widget);
  133.     
  134.     XtPopdown(popup->shell_widget);
  135. }
  136.  
  137. unsigned long PopupDialog(popup, message, suggestion, answer, grab)
  138.     Dialog popup;
  139.     String message, suggestion, *answer;
  140.     XtGrabKind grab;
  141. {
  142.   Position popup_x, popup_y, top_x, top_y;
  143.   Dimension popup_width, popup_height, top_width, top_height, border_width;
  144.   int n;
  145.   Arg wargs[4];
  146.  
  147.   n = 0;
  148.   XtSetArg(wargs[n], XtNlabel, message); n++;
  149.   XtSetArg(wargs[n], XtNvalue, suggestion); n++;
  150.   XtSetValues(popup->dialog_widget, wargs, n);
  151.  
  152.   XtRealizeWidget(popup->shell_widget);
  153.  
  154.   n = 0;
  155.   XtSetArg(wargs[n], XtNx, &top_x); n++;
  156.   XtSetArg(wargs[n], XtNy, &top_y); n++;
  157.   XtSetArg(wargs[n], XtNwidth, &top_width); n++;
  158.   XtSetArg(wargs[n], XtNheight, &top_height); n++;
  159.   XtGetValues(popup->top_widget, wargs, n);
  160.  
  161.   n = 0;
  162.   XtSetArg(wargs[n], XtNwidth, &popup_width); n++;
  163.   XtSetArg(wargs[n], XtNheight, &popup_height); n++;
  164.   XtSetArg(wargs[n], XtNborderWidth, &border_width); n++;
  165.   XtGetValues(popup->shell_widget, wargs, n);
  166.  
  167.   popup_x = max(0, 
  168.     min(top_x + ((Position)top_width - (Position)popup_width) / 2, 
  169.         (Position)DisplayWidth(XtDisplay(popup->shell_widget), 
  170.            DefaultScreen(XtDisplay(popup->shell_widget))) -
  171.         (Position)popup_width - 2 * (Position)border_width));
  172.   popup_y = max(0, 
  173.     min(top_y + ((Position)top_height - (Position)popup_height) / 2,
  174.         (Position)DisplayHeight(XtDisplay(popup->shell_widget), 
  175.             DefaultScreen(XtDisplay(popup->shell_widget))) -
  176.         (Position)popup_height - 2 * (Position)border_width));
  177.   n = 0;
  178.   XtSetArg(wargs[n], XtNx, popup_x); n++;
  179.   XtSetArg(wargs[n], XtNy, popup_y); n++;
  180.   XtSetValues(popup->shell_widget, wargs, n);
  181.  
  182.   selected = None;
  183.  
  184.   XtPopup(popup->shell_widget, grab);
  185.   XWarpPointer(XtDisplay(popup->shell_widget), 
  186.            XtWindow(popup->top_widget),
  187.            XtWindow(popup->shell_widget), 
  188.            0, 0, top_width, top_height,
  189.            popup_width / 2, popup_height / 2);
  190.  
  191.   while ((selected & popup->options) == None) {
  192.       XEvent event;
  193.       XtNextEvent(&event);
  194.       XtDispatchEvent(&event);
  195.   }
  196.  
  197.   PopdownDialog(popup, answer);
  198.  
  199.   return (selected & popup->options);
  200. }
  201.